﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class SchoolManagement_CreateRoom : AdminUserBase
//public partial class SchoolMamagement_createRoom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPageData();
            
 
        }
    }

    private void BindPageData()
    {
        #region 绑定学校下拉列表
        TeacherModule ta = new TeacherModule();
        List<V_School> vs = new List<V_School>();
        vs = ta.GetSchool();
        dlSchool.DataSource = vs;
        dlSchool.DataBind();
        #endregion

        #region 绑定SVR列表
        SVR_IPC_Module svr = new SVR_IPC_Module();
        DataSet dsSvrList = svr.GetSVRInfoList("");
        ddlSVRList.DataSource = dsSvrList;
        ddlSVRList.DataValueField = "SVRID";
        ddlSVRList.DataTextField = "SVRName";
        ddlSVRList.DataBind();
     
        #endregion

        if (this.CurrentUser.SchoolID != 0)
        {
            dlSchool.SelectedValue = this.SchoolID.ToString();
            dlSchool.Enabled = false;
            //摄像头控制，运营商能修改
            ddlCameraStatus.SelectedValue = "1";
            ddlCameraStatus.Enabled = false;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {


        ClassRoomModule ra = new ClassRoomModule();
        V_room room = new V_room();
        IPCInfo_Table ipcModel = new IPCInfo_Table();
        SVR_IPC_Relation svrIpcRelation=new SVR_IPC_Relation();
        ipcModel.IPCStatus = int.Parse(ddlCameraStatus.SelectedValue);
        room.SchoolID= int.Parse(dlSchool.SelectedValue);
        room.VR_state = int.Parse( ddlCameraStatus.SelectedValue);
        room.VR_roomName = this.txtVR_roomName.Text;
        room.VR_roomCode = this.txtVR_roomCode.Text;
        room.VR_cameraPN = this.txtIPCName.Text;
        room.VR_cameraCode = this.txtVR_cameraCode.Text;
        room.VR_cameraImgSize = this.txtVR_cameraImgSize.Text;
        ipcModel.IPCName = this.txtIPCName.Text;
        ipcModel.IPCIp = this.txtIPCIp.Text.ToString();
        ipcModel.IPCPort = !string.IsNullOrEmpty(txtIPCPort.Text) ? StringUtils.IsNumeric(txtIPCPort.Text) ? Convert.ToInt32(txtIPCPort.Text) : 0 : 0;
        ipcModel.IPCUser = this.txtIPCUser.Text.ToString();
        ipcModel.IPCPwd = this.txtIPCPwd.Text.ToString();
        svrIpcRelation.RelationPipe = !string.IsNullOrEmpty(txtSvrPort.Value) ? StringUtils.IsNumeric(txtSvrPort.Value) ? Convert.ToInt32(txtSvrPort.Value) : 0 : 0;
        svrIpcRelation.RelationComment = txtVR_roomCode.Text;
        svrIpcRelation.SVRID = string.IsNullOrEmpty(ddlSVRList.SelectedValue)
                                   ? 0
                                   : Convert.ToInt32(ddlSVRList.SelectedValue);
        DataSet dataSet = ra.ChkSvrPort(svrIpcRelation.RelationPipe.ToString(), svrIpcRelation.SVRID.ToString());
        if (dataSet.Tables[0].Rows.Count > 0)
        {
            Page.RegisterStartupScript("", "<script>alert('对不起，"+ddlSVRList.SelectedItem.Text+"的端口号：" + svrIpcRelation.RelationPipe.ToString() + "，被" + dataSet.Tables[0].Rows[0]["Vs_name"] + "学校的：" + dataSet.Tables[0].Rows[0]["VR_roomName"] + "教室占用！');</script>");
             return;
            

        }


        int i = ra.AddNewRoom(room, ipcModel,svrIpcRelation);
        if (i > 1)
        {
            lblMess.Text = "添加成功";
            if (this.SchoolID == 0)
            {
                Response.Redirect("SearchRoomList.aspx");
            }
            else
            { Response.Redirect("SchoolAdminSearcherRoomList.aspx"); }
        }
        else
        {
             lblMess.Text = "添加失败";
        }

    }
    protected void txtVR_roomCode_TextChanged(object sender, EventArgs e)
    {
        string roomcode = txtVR_roomCode.Text;
        ClassRoomModule ra = new ClassRoomModule();
       DataSet ds= ra.GetRoomListByWhereAndSID(" and  VR_roomCode='" + roomcode+"'", CurrentUser.SchoolID);
        if(ds.Tables[0].Rows.Count>0)
        {
            Page.RegisterStartupScript("", "<script>alert('对不起教室编号不能重复！')</script>");
            return;
           
        }
    }
}
